import {login, logout, getInfo, register, wxlogin, wxh5login} from '@/api/login'
import {getToken, setToken, removeToken} from '@/utils/auth'
// import defAva from '@/assets/images/profile.jpg'
import {defineStore} from 'pinia'
const defAva = '/static/img/pin-dao.png'
const useUserStore = defineStore(
    'user',
    {
        state: () => ({
            token: getToken(),
            name: '',
            avatar: '',
            pingdaoAvatar: defAva,
            roles: ['ROLE_DEFAULT'],
            permissions: [],
            userData: {}
        }),
        actions: {
            // 登录
            login(userInfo) {
                const username = userInfo.username.trim()
                const password = userInfo.password
                const code = userInfo.code
                const uuid = userInfo.uuid
                return new Promise((resolve, reject) => {
                    login(username, password, code, uuid).then(res => {
                        if (res.code !== 0) {
                            return reject(res)
                        }
                        const token = res.data?.token
                        setToken(token)
                        this.token = token
                        resolve()
                    }).catch(error => {
                        reject(error)
                    })
                })
            },
            // 微信登录
            wxlogin(data) {
                return new Promise((resolve, reject) => {
                    wxlogin(data).then(res => {
                        const token = res.data.token
                        setToken(token)
                        this.token = token
                        resolve()
                    }).catch(error => {
                        reject(error)
                    })
                })
            },
            wxh5login(data) {
                return new Promise((resolve, reject) => {
                    wxh5login(data).then(res => {
                        const token = res.data.token
                        setToken(token)
                        this.token = token
                        resolve()
                    }).catch(error => {
                        reject(error)
                    })
                })
            },
            register(data) {
                return new Promise((resolve, reject) => {
                    register(data).then(res => {
                        resolve(res)
                    }).catch(error => {
                        reject(error)
                    })
                })
            },
            // 获取用户信息
            getInfo() {
                return new Promise((resolve, reject) => {
                    getInfo().then(res => {
                        const user = {...res.data.userinfo, ...res.data.user_asset}
                        const avatar = (user.user_photo == "" || user.user_photo == null) ? defAva : user.user_photo;
                        user.allNum = (user.giving_num || 0) + (user.experience_num || 0) + (user.remaining_num || 0)
                        this.userData = user
                        this.name = user.user_name
                        this.avatar = avatar;
                        resolve(res)
                    }).catch(error => {
                        reject(error)
                    })
                })
            },
            // 退出系统
            logOut() {
                return new Promise((resolve, reject) => {
                    logout(this.token).then(() => {
                        this.token = ''
                        this.roles = []
                        this.permissions = []
                        removeToken()
                        resolve()
                    }).catch(error => {
                        reject(error)
                    })
                })
            },
            // 前端登出
            fedLogOut() {
                return new Promise(resolve => {
                    this.token = ''
                    this.roles = []
                    this.permissions = []
                    removeToken()
                    resolve()
                })
            }
        }
    })

export default useUserStore
